home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / drawgrid.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  25.4 KB  |  1,024 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. IMPLEMENT_SERIAL( CDrawingObjectGrid, CObject, 1 )
  18.  
  19. CDrawingObjectGrid::CDrawingObjectGrid()
  20. {
  21.    m_Initialize();
  22. }
  23.  
  24. CDrawingObjectGrid::CDrawingObjectGrid( DWORD number_of_rows, DWORD number_of_columns )
  25. {
  26.    m_Initialize();
  27.    SetSize( number_of_rows, number_of_columns );
  28. }
  29.  
  30. CDrawingObjectGrid::~CDrawingObjectGrid()
  31. {
  32.    RemoveAll();
  33. }
  34.  
  35. void CDrawingObjectGrid::Draw( CDC& device_context )
  36. {
  37.    int index              = 0;
  38.    int number_of_elements = m_ObjectArray.GetSize();
  39.  
  40.    CRectangle *object_p = (CRectangle *) NULL;
  41.  
  42.    while( index < number_of_elements )
  43.    {
  44.       object_p = (CRectangle *) m_ObjectArray[ index ];
  45.       
  46.       if ( object_p != NULL )
  47.       {
  48.          object_p->Draw( device_context );
  49.       }
  50.  
  51.       index++;
  52.    }
  53. }
  54.  
  55. CRectangle*& CDrawingObjectGrid::ElementAt( DWORD row_number, DWORD column_number )
  56. {
  57.    return( (CRectangle *&) m_ObjectArray.ElementAt( ( row_number * m_NumberOfColumns ) + column_number ) );
  58. }
  59.  
  60. CRectangle* CDrawingObjectGrid::GetAt( DWORD row_number, DWORD column_number )
  61. {
  62.    return( (CRectangle *) m_ObjectArray.ElementAt( ( row_number * m_NumberOfColumns ) + column_number ) );
  63. }
  64.  
  65. DWORD CDrawingObjectGrid::GetHeight( void ) const
  66. {
  67.    DWORD height = 0;
  68.    DWORD index  = 0;
  69.  
  70.    CRectangle *object_p = (CRectangle *) NULL;
  71.  
  72.    while( index < m_NumberOfRows )
  73.    {
  74.       object_p = (CRectangle *) m_ObjectArray[ index ];
  75.  
  76.       if ( object_p != NULL )
  77.       {
  78.          height += object_p->GetHeight();
  79.          height += m_VerticalSpacing;
  80.       }
  81.  
  82.       index++;
  83.    }
  84.  
  85.    if ( m_NumberOfRows > 0 )
  86.    {
  87.       height -= m_VerticalSpacing;
  88.    }
  89.  
  90.    return( height );
  91. }
  92.  
  93. int CDrawingObjectGrid::GetHorizontalSpacing( void ) const
  94. {
  95.    return( m_HorizontalSpacing );
  96. }
  97.  
  98. BOOL CDrawingObjectGrid::GetIndexFromPoint( DWORD& _row_index, DWORD& _column_index, const CPoint& point )
  99. {
  100.    DWORD row_index    = 0;
  101.    DWORD column_index = 0;
  102.  
  103.    CRectangle *object_p = (CRectangle *) NULL;
  104.  
  105.    CRect rectangle;
  106.  
  107.    while( row_index < (DWORD) m_NumberOfRows )
  108.    {
  109.       column_index = 0;
  110.  
  111.       while( column_index < (DWORD) m_NumberOfColumns )
  112.       {
  113.          object_p = GetAt( row_index, column_index );
  114.  
  115.          if ( object_p != NULL )
  116.          {
  117.             object_p->GetRectangle( rectangle );
  118.  
  119.             if ( rectangle.PtInRect( point ) == TRUE )
  120.             {
  121.                _row_index    = row_index;
  122.                _column_index = column_index;
  123.                return( TRUE );
  124.             }
  125.          }
  126.  
  127.          column_index++;
  128.       }
  129.  
  130.       row_index++;
  131.    }
  132.  
  133.    return( FALSE );
  134. }
  135.  
  136. void CDrawingObjectGrid::GetName( CString& name_of_grid ) const
  137. {
  138.    name_of_grid = m_Name;
  139. }
  140.  
  141. DWORD CDrawingObjectGrid::GetNumberOfColumns( void ) const
  142. {
  143.    return( m_NumberOfColumns );
  144. }
  145.  
  146. DWORD CDrawingObjectGrid::GetNumberOfRows( void ) const
  147. {
  148.    return( m_NumberOfRows );
  149. }
  150.  
  151. void CDrawingObjectGrid::GetRectangle( CRect& rectangle ) const
  152. {
  153.    if ( m_ObjectArray[ 0 ] == NULL )
  154.    {
  155.       rectangle.SetRectEmpty();
  156.       return;
  157.    }
  158.  
  159.    CRectangle *object_p = (CRectangle *) m_ObjectArray[ 0 ];
  160.  
  161.    if ( object_p == NULL )
  162.    {
  163.       rectangle.SetRectEmpty();
  164.       return;
  165.    }
  166.  
  167.    CRect object_rectangle;
  168.  
  169.    object_p->GetRectangle( object_rectangle );
  170.  
  171.    rectangle.left   = object_rectangle.left;
  172.    rectangle.top    = object_rectangle.top;
  173.    rectangle.right  = rectangle.left + GetWidth();
  174.    rectangle.bottom = rectangle.top  + GetHeight();
  175. }
  176.  
  177. DWORD CDrawingObjectGrid::GetVerticalSpacing( void ) const
  178. {
  179.    return( m_VerticalSpacing );
  180. }
  181.  
  182. DWORD CDrawingObjectGrid::GetWidth( void ) const
  183. {
  184.    DWORD width = 0;
  185.    DWORD index = 0;
  186.  
  187.    CRectangle *object_p = NULL;
  188.  
  189.    while( index < m_NumberOfColumns )
  190.    {
  191.       object_p = (CRectangle *) m_ObjectArray[ index ];
  192.  
  193.       if ( object_p != NULL )
  194.       {
  195.          width += object_p->GetWidth();
  196.          width += m_HorizontalSpacing;
  197.       }
  198.  
  199.       index++;
  200.    }
  201.  
  202.    if ( m_NumberOfColumns > 0 )
  203.    {
  204.       width -= m_HorizontalSpacing;
  205.    }
  206.  
  207.    return( width );
  208. }
  209.  
  210. void CDrawingObjectGrid::m_Initialize( void )
  211. {
  212.    RemoveAll();
  213.    m_VerticalSpacing   = 1;
  214.    m_HorizontalSpacing = 1;
  215.    m_NumberOfRows      = 0;
  216.    m_NumberOfColumns   = 0;
  217.  
  218.    m_Name.Empty();
  219. }
  220.  
  221. void CDrawingObjectGrid::RemoveAll( void )
  222. {
  223.    int index = 0;
  224.    int number_of_elements = m_ObjectArray.GetSize();
  225.  
  226.    CRectangle *object_p = NULL;
  227.  
  228.    while( index < number_of_elements )
  229.    {
  230.       object_p = (CRectangle *) m_ObjectArray[ index ];
  231.  
  232.       if ( object_p != NULL )
  233.       {
  234.          delete object_p;
  235.       }
  236.  
  237.       index++;
  238.    }
  239.  
  240.    m_ObjectArray.RemoveAll();
  241.  
  242.    m_NumberOfRows    = 0;
  243.    m_NumberOfColumns = 0;
  244. }
  245.  
  246. void CDrawingObjectGrid::Serialize( CArchive& archive )
  247. {
  248.    CObject::Serialize( archive );
  249.  
  250.    if ( archive.IsStoring() )
  251.    {
  252.       archive << m_NumberOfRows;
  253.       archive << m_NumberOfColumns;
  254.       archive << m_VerticalSpacing;
  255.       archive << m_HorizontalSpacing;
  256.       archive << m_Name;
  257.    }
  258.    else
  259.    {
  260.       archive >> m_NumberOfRows;
  261.       archive >> m_NumberOfColumns;
  262.       archive >> m_VerticalSpacing;
  263.       archive >> m_HorizontalSpacing;
  264.       archive >> m_Name;
  265.    }
  266.  
  267.    m_ObjectArray.Serialize( archive );
  268. }
  269.  
  270. void CDrawingObjectGrid::SetAt( DWORD row_number, DWORD column_number, CRectangle *new_element )
  271. {
  272.    DWORD index = ( row_number * m_NumberOfColumns ) + column_number;
  273.  
  274.    CRectangle *object_p = (CRectangle *) m_ObjectArray[ index ];
  275.  
  276.    if ( object_p != NULL )
  277.    {
  278.       if ( object_p == new_element )
  279.       {
  280.          return;
  281.       }
  282.       else
  283.       {
  284.          delete object_p;
  285.       }
  286.    }
  287.  
  288.    m_ObjectArray[ index ] = new_element;
  289. }
  290.  
  291. void CDrawingObjectGrid::SetFillColor( DWORD row_number, DWORD column_number, COLORREF color )
  292. {
  293.    CRectangle *object_p = GetAt( row_number, column_number );
  294.  
  295.    if ( object_p != NULL )
  296.    {
  297.       object_p->SetFillColor( color );
  298.    }
  299. }
  300.  
  301. void CDrawingObjectGrid::SetHorizontalSpacing( DWORD horizontal_spacing )
  302. {
  303.    m_HorizontalSpacing = horizontal_spacing;
  304. }
  305.  
  306. void CDrawingObjectGrid::SetLineColor( DWORD row_number, DWORD column_number, COLORREF color )
  307. {
  308.    CRectangle *object_p = GetAt( row_number, column_number );
  309.  
  310.    if ( object_p != NULL )
  311.    {
  312.       object_p->SetLineColor( color );
  313.    }
  314. }
  315.  
  316. void CDrawingObjectGrid::SetName( LPCTSTR name_of_grid )
  317. {
  318.    if ( name_of_grid != NULL )
  319.    {
  320.       m_Name = name_of_grid;
  321.    }
  322.    else
  323.    {
  324.       m_Name.Empty();
  325.    }
  326. }
  327.  
  328. void CDrawingObjectGrid::SetRectangle( const CRect& source )
  329. {
  330.    if ( m_ObjectArray[ 0 ] == NULL )
  331.    {
  332.       return;
  333.    }
  334.  
  335.    CRectangle *object_p = (CRectangle *) m_ObjectArray[ 0 ];
  336.  
  337.    if ( object_p == NULL )
  338.    {
  339.       return;
  340.    }
  341.  
  342.    DWORD vertical_size   = object_p->GetHeight();
  343.    DWORD horizontal_size = object_p->GetWidth();
  344.  
  345.    DWORD vertical_location   = source.top;
  346.    DWORD horizontal_location = source.left;
  347.  
  348.    DWORD column_index = 0;
  349.    DWORD row_index    = 0;
  350.  
  351.    CPoint point;
  352.  
  353.    while( row_index < m_NumberOfRows )
  354.    {
  355.       column_index        = 0;
  356.       horizontal_location = source.left;
  357.  
  358.       while( column_index < m_NumberOfColumns )
  359.       {
  360.          point.x = horizontal_location;
  361.          point.y = vertical_location;
  362.  
  363.          object_p = GetAt( row_index, column_index );
  364.  
  365.          if ( object_p != NULL )
  366.          {
  367.             object_p->SetLocation( point );
  368.          }
  369.  
  370.          horizontal_location += (horizontal_size + m_HorizontalSpacing );
  371.          column_index++;
  372.       }
  373.  
  374.       vertical_location += ( vertical_size + m_VerticalSpacing );
  375.       row_index++;
  376.    }
  377. }
  378.  
  379. void CDrawingObjectGrid::SetSize( DWORD number_of_rows, DWORD number_of_columns )
  380. {
  381.    RemoveAll();
  382.  
  383.    m_NumberOfRows    = number_of_rows;
  384.    m_NumberOfColumns = number_of_columns;
  385.    m_ObjectArray.SetSize( m_NumberOfRows * m_NumberOfColumns );
  386. }
  387.  
  388. void CDrawingObjectGrid::SetVerticalSpacing( DWORD vertical_spacing )
  389. {
  390.    m_VerticalSpacing = vertical_spacing;
  391. }
  392.  
  393. /*
  394. ** And now for some labels
  395. */
  396.  
  397. IMPLEMENT_SERIAL( CLabeledGrid, CDrawingObjectGrid, 1 )
  398.  
  399. CLabeledGrid::CLabeledGrid()
  400. {
  401.    m_Initialize();
  402. }
  403.  
  404. CLabeledGrid::CLabeledGrid( DWORD number_of_rows, DWORD number_of_columns )
  405. {
  406.    m_Initialize();
  407.    SetSize( number_of_rows, number_of_columns );
  408. }
  409.  
  410. CLabeledGrid::~CLabeledGrid()
  411. {
  412.    m_RowNamesFont.DeleteObject();
  413.    m_RowsTitleFont.DeleteObject();
  414.    m_ColumnNamesFont.DeleteObject();
  415.    m_ColumnsTitleFont.DeleteObject();
  416. }
  417.  
  418. void CLabeledGrid::Draw( CDC& device_context )
  419. {
  420.    /*
  421.    ** Go draw the CRectangles...
  422.    */
  423.  
  424.    CDrawingObjectGrid::Draw( device_context );
  425.  
  426.    int x_coordinate         = 0;
  427.    int y_coordinate         = 0;
  428.    int object_height        = 0;
  429.    int object_width         = 0;
  430.    int label_height         = 0;
  431.    int label_width          = 0;
  432.    int column_labels_offset = 0;
  433.    int row_labels_offset    = 0;
  434.  
  435.    CRectangle *rectangle_p = NULL;
  436.  
  437.    CRect rect;
  438.  
  439.    CString name;
  440.  
  441.    CSize size;
  442.  
  443.    DWORD index = 0;
  444.  
  445.    CFont *old_font = (CFont *) NULL;
  446.  
  447.    CString grid_name;
  448.  
  449.    old_font = device_context.SelectObject( &m_RowNamesFont );
  450.  
  451.    rectangle_p = GetAt( 0, 0 );
  452.    rectangle_p->GetRectangle( rect );
  453.  
  454.    if ( m_LabelOptions & LABELED_GRID_ROW_NAMES )
  455.    {
  456.       index = 0;
  457.  
  458.       device_context.SelectObject( &m_RowNamesFont );
  459.  
  460.       while( index < m_NumberOfRows )
  461.       {
  462.          rectangle_p = GetAt( index, 0 );
  463.  
  464.          rectangle_p->GetRectangle( rect );
  465.  
  466.          /*
  467.          ** Get the rectangle for the actual drawing objects...
  468.          **
  469.          **                rect.left (x_coordinate)
  470.          **                |
  471.          **                v
  472.          **     rect.top ->+-----+   +-----+   +-----+
  473.          ** (y_coordinate) |     |   |     |   |     |
  474.          **                | 0,0 |   | 0,1 |   | 0,2 |
  475.          **                |     |   |     |   |     |
  476.          **                +-----+   +-----+   +-----+
  477.          **
  478.          **                +-----+   +-----+   +-----+
  479.          **                |     |   |     |   |     |
  480.          **                | 1,0 |   | 1,1 |   | 1,2 |
  481.          **                |     |   |     |   |     |
  482.          **                +-----+   +-----+   +-----+
  483.          **
  484.          **                +-----+   +-----+   +-----+
  485.          **                |     |   |     |   |     |
  486.          **                | 2,0 |   | 2,1 |   | 2,2 |
  487.          **                |     |   |     |   |     |
  488.          **                +-----+   +-----+   +-----+
  489.          */
  490.  
  491.          x_coordinate = rect.left;
  492.          y_coordinate = rect.top;
  493.  
  494.          /*
  495.          ** Move the x_coordinate to the left to make room for the longest row title (the longest row title was determined
  496.          ** in the call to PrepareForPainting().
  497.          **
  498.          ** Horizontal Spacing is the distance betweeen drawing objects in the grid:
  499.          **
  500.          **                        m_HorizontalSpacing
  501.          **                        |
  502.          **                        v
  503.          **                       |-|
  504.          **                +-----+   +-----+   +-----+
  505.          **                |     |   |     |   |     |
  506.          **                | 0,0 |   | 0,1 |   | 0,2 |
  507.          **                |     |   |     |   |     |
  508.          **                +-----+   +-----+   +-----+
  509.          **
  510.          **                +-----+   +-----+   +-----+
  511.          **                |     |   |     |   |     |
  512.          **                | 1,0 |   | 1,1 |   | 1,2 |
  513.          **                |     |   |     |   |     |
  514.          **                +-----+   +-----+   +-----+
  515.          **
  516.          **                +-----+   +-----+   +-----+
  517.          **                |     |   |     |   |     |
  518.          **                | 2,0 |   | 2,1 |   | 2,2 |
  519.          **                |     |   |     |   |     |
  520.          **                +-----+   +-----+   +-----+
  521.          */
  522.  
  523.          x_coordinate -= ( m_NumberOfLogicalUnitsInLongestRowName + ( m_HorizontalSpacing * 2 ) + 1 );
  524.  
  525.          /*
  526.          ** Try to vertically center the row label in the space provided by the height of the object
  527.          */
  528.  
  529.          GetRowName( index, name );
  530.  
  531.          size = device_context.GetTextExtent( name, name.GetLength() );
  532.  
  533.          label_height  = size.cy;
  534.          object_height = rect.Height();
  535.  
  536.          y_coordinate += ( object_height / 2 );
  537.          y_coordinate -= ( label_height  / 2 );
  538.  
  539.          /*
  540.          ** Make sure we didn't go above the object
  541.          */
  542.  
  543.          if ( y_coordinate < rect.top )
  544.          {
  545.             y_coordinate = rect.top;
  546.          }
  547.  
  548.          /*
  549.          ** Now store where we are in case we have to paint the rows title
  550.          */
  551.  
  552.          row_labels_offset = x_coordinate - ( m_HorizontalSpacing + 1 );
  553.  
  554.          device_context.TextOut( x_coordinate, y_coordinate, name );
  555.  
  556.          index++;
  557.       }
  558.    }
  559.  
  560.    if ( m_LabelOptions & LABELED_GRID_COLUMN_NAMES )
  561.    {
  562.       index = 0;
  563.  
  564.       device_context.SelectObject( &m_ColumnNamesFont );
  565.  
  566.       while( index < m_NumberOfColumns )
  567.       {
  568.          rectangle_p = GetAt( 0, index );
  569.  
  570.          rectangle_p->GetRectangle( rect );
  571.  
  572.          GetColumnName( index, name );
  573.  
  574.          /*
  575.          ** Try to center the label
  576.          */
  577.  
  578.          x_coordinate = rect.left;
  579.  
  580.          size = device_context.GetTextExtent( name, name.GetLength() );
  581.  
  582.          column_labels_offset = m_VerticalSpacing + size.cy + 1;
  583.          y_coordinate = rect.top - column_labels_offset;
  584.  
  585.          if ( y_coordinate < 0 )
  586.          {
  587.             y_coordinate = 0;
  588.          }
  589.          
  590.          label_width  = size.cx;
  591.          object_width = rect.Width();
  592.  
  593.          x_coordinate += ( object_width / 2 );
  594.          x_coordinate -= ( label_width  / 2 );
  595.  
  596.          if ( x_coordinate < rect.left )
  597.          {
  598.             x_coordinate = rect.left;
  599.          }
  600.  
  601.          device_context.TextOut( x_coordinate, y_coordinate, name );
  602.  
  603.          index++;
  604.       }
  605.    }
  606.  
  607.    if ( m_LabelOptions & LABELED_GRID_COLUMNS_TITLE )
  608.    {
  609.       /*
  610.       ** Get the rectangle for the entire grid
  611.       */
  612.  
  613.       GetRectangle( rect );
  614.  
  615.       x_coordinate  = rect.left;
  616.       x_coordinate += rect.Width() / 2;
  617.       x_coordinate -= m_NumberOfLogicalUnitsInColumnsTitle / 2;
  618.  
  619.       GetColumnsTitle( name );
  620.       device_context.SelectObject( &m_ColumnsTitleFont );
  621.  
  622.       size = device_context.GetTextExtent( name, name.GetLength() );
  623.       
  624.       y_coordinate = rect.top - ( size.cy + column_labels_offset + m_VerticalSpacing + 1 );
  625.  
  626.       if ( y_coordinate < 0 )
  627.       {
  628.          y_coordinate = 0;
  629.       }
  630.  
  631.       device_context.TextOut( x_coordinate, y_coordinate, name );
  632.    }
  633.  
  634.    if ( m_LabelOptions & LABELED_GRID_ROWS_TITLE )
  635.    {
  636.       /*
  637.       ** Get the rectangle for the entire grid
  638.       */
  639.  
  640.       GetRectangle( rect );
  641.  
  642.       /*
  643.       ** Get the rectangle for the actual drawing objects...
  644.       **
  645.       **                rect.left (x_coordinate)
  646.       **                |
  647.       **                v
  648.       **                +-----+   +-----+   +-----+
  649.       **                |     |   |     |   |     |
  650.       **                | 0,0 |   | 0,1 |   | 0,2 |
  651.       **                |     |   |     |   |     |
  652.       **                +-----+   +-----+   +-----+
  653.       **
  654.       **                +-----+   +-----+   +-----+
  655.       **                |     |   |     |   |     |
  656.       **                | 1,0 |   | 1,1 |   | 1,2 |
  657.       **                |     |   |     |   |     |
  658.       **                +-----+   +-----+   +-----+
  659.       **
  660.       **                +-----+   +-----+   +-----+
  661.       **                |     |   |     |   |     |
  662.       **                | 2,0 |   | 2,1 |   | 2,2 |
  663.       **                |     |   |     |   |     |
  664.       **  rect.bottom ->+-----+   +-----+   +-----+
  665.       **  (y_coordinate)
  666.       */
  667.  
  668.       x_coordinate = rect.left;
  669.       y_coordinate = rect.bottom;
  670.  
  671.       GetRowsTitle( name );
  672.       device_context.SelectObject( &m_RowsTitleFont );
  673.  
  674.       size = device_context.GetTextExtent( name, name.GetLength() );
  675.  
  676.       /*
  677.       ** Now this is a little weird, remember that the rows title is turned so that it runs from the bottom
  678.       ** of the grid to the top. This means we need to move to the left of the grid by the number of logical
  679.       ** units in the *height* of the title. Usually we don't care about the height of a title when dealing
  680.       ** in the x-axis but since we turned the font so it will print vertically, the height of the title 
  681.       ** becomes the width (strange but true...).
  682.       */
  683.  
  684.       label_height = size.cy;
  685.  
  686.       //x_coordinate -= ( row_labels_offset + m_HorizontalSpacing + label_height + 1 );
  687.       x_coordinate -= ( row_labels_offset + label_height + m_HorizontalSpacing );
  688.  
  689.       y_coordinate -= rect.Height() / 2;
  690.       y_coordinate += m_NumberOfLogicalUnitsInRowsTitle / 2;
  691.  
  692.       device_context.TextOut( x_coordinate, y_coordinate, name );
  693.    }
  694.  
  695.    device_context.SelectObject( old_font );
  696. }
  697.  
  698. void CLabeledGrid::GetColumnName( DWORD column_number, CString& column_name ) const
  699. {
  700.    if ( column_number >= (DWORD) m_ColumnNames.GetSize() )
  701.    {
  702.       column_name.Empty();
  703.       return;
  704.    }
  705.  
  706.    column_name = m_ColumnNames[ column_number ];
  707. }
  708.  
  709. void CLabeledGrid::GetColumnsTitle( CString& columns_title ) const
  710. {
  711.    columns_title = m_ColumnsTitle;
  712. }
  713.  
  714. void CLabeledGrid::GetRowName( DWORD row_number, CString& row_name ) const
  715. {
  716.    if ( row_number >= (DWORD) m_RowNames.GetSize() )
  717.    {
  718.       row_name.Empty();
  719.       return;
  720.    }
  721.  
  722.    row_name = m_RowNames[ row_number ];
  723. }
  724.  
  725. void CLabeledGrid::GetRowsTitle( CString& rows_title ) const
  726. {
  727.    rows_title = m_RowsTitle;
  728. }
  729.  
  730. void CLabeledGrid::m_Initialize( void )
  731. {
  732.    m_ColumnFontSize                          = 0;
  733.    m_ColumnsTitleFontSize                    = 0;
  734.    m_RowFontSize                             = 0;
  735.    m_RowsTitleFontSize                       = 0;
  736.    m_NumberOfLogicalUnitsInLongestRowName    = 0;
  737.    m_NumberOfLogicalUnitsInLongestColumnName = 0;
  738.    m_NumberOfLogicalUnitsInRowsTitle         = 0;
  739.    m_NumberOfLogicalUnitsInColumnsTitle      = 0;
  740.    m_LabelOptions                            = 0;
  741.    m_RowsTitle.Empty();
  742.    m_ColumnsTitle.Empty();
  743. }
  744.  
  745. void CLabeledGrid::m_SetColumnFontSize( CDC& device_context, DWORD font_size )
  746. {
  747.    m_ColumnFontSize = font_size;
  748.  
  749.    TRY
  750.    {
  751.       LOGFONT lf;
  752.  
  753.       ::ZeroMemory( &lf, sizeof( lf ) );
  754.  
  755.       lf.lfHeight         = -::MulDiv( m_ColumnFontSize, device_context.GetDeviceCaps( LOGPIXELSY ), 72 );
  756.       lf.lfCharSet        = DEFAULT_CHARSET;
  757.       lf.lfQuality        = DEFAULT_QUALITY;
  758.       lf.lfWeight         = FW_NORMAL;
  759.       lf.lfClipPrecision  = CLIP_LH_ANGLES | CLIP_STROKE_PRECIS;
  760.       lf.lfPitchAndFamily = FF_SWISS;
  761.       lf.lfEscapement     = 0;
  762.  
  763.       m_ColumnNamesFont.CreateFontIndirect( &lf );
  764.    }
  765.    CATCH( CResourceException, e )
  766.    {
  767.       TRACE( "CLabeledGrid::m_SetColumnFontSize(), font creation failed\n" );
  768.       return;
  769.    }
  770.    END_CATCH
  771. }
  772.  
  773. void CLabeledGrid::m_SetColumnsTitleFontSize( CDC& device_context, DWORD font_size )
  774. {
  775.    m_ColumnsTitleFontSize = font_size;
  776.  
  777.    TRY
  778.    {
  779.       LOGFONT lf;
  780.  
  781.       ::ZeroMemory( &lf, sizeof( lf ) );
  782.  
  783.       lf.lfHeight         = -::MulDiv( m_ColumnsTitleFontSize, device_context.GetDeviceCaps( LOGPIXELSY ), 72 );
  784.       lf.lfCharSet        = DEFAULT_CHARSET;
  785.       lf.lfQuality        = DEFAULT_QUALITY;
  786.       lf.lfWeight         = FW_NORMAL;
  787.       lf.lfClipPrecision  = CLIP_LH_ANGLES | CLIP_STROKE_PRECIS;
  788.       lf.lfPitchAndFamily = FF_SWISS;
  789.       lf.lfEscapement     = 0;
  790.  
  791.       m_ColumnsTitleFont.CreateFontIndirect( &lf );
  792.    }
  793.    CATCH( CResourceException, e )
  794.    {
  795.       TRACE( "CLabeledGrid::m_SetColumnsTitleFontSize(), font creation failed\n" );
  796.       return;
  797.    }
  798.    END_CATCH
  799. }
  800.  
  801. void CLabeledGrid::m_SetRowFontSize( CDC& device_context, DWORD font_size )
  802. {
  803.    m_RowFontSize = font_size;
  804.  
  805.    TRY
  806.    {
  807.       LOGFONT lf;
  808.  
  809.       ::ZeroMemory( &lf, sizeof( lf ) );
  810.  
  811.       lf.lfHeight         = -::MulDiv( m_RowFontSize, device_context.GetDeviceCaps( LOGPIXELSY ), 72 );
  812.       lf.lfCharSet        = DEFAULT_CHARSET;
  813.       lf.lfQuality        = DEFAULT_QUALITY;
  814.       lf.lfWeight         = FW_NORMAL;
  815.       lf.lfClipPrecision  = CLIP_LH_ANGLES | CLIP_STROKE_PRECIS;
  816.       lf.lfPitchAndFamily = FF_SWISS;
  817.       lf.lfEscapement     = 0;
  818.  
  819.       m_RowNamesFont.CreateFontIndirect( &lf );
  820.    }
  821.    CATCH( CResourceException, e )
  822.    {
  823.       TRACE( "CLabeledGrid::m_SetColumnFontSize(), font creation failed\n" );
  824.       return;
  825.    }
  826.    END_CATCH
  827. }
  828.  
  829. void CLabeledGrid::m_SetRowsTitleFontSize( CDC& device_context, DWORD font_size )
  830. {
  831.    m_RowsTitleFontSize = font_size;
  832.  
  833.    TRY
  834.    {
  835.       LOGFONT lf;
  836.  
  837.       ::ZeroMemory( &lf, sizeof( lf ) );
  838.  
  839.       lf.lfHeight         = -::MulDiv( m_RowsTitleFontSize, device_context.GetDeviceCaps( LOGPIXELSY ), 72 );
  840.       lf.lfCharSet        = DEFAULT_CHARSET;
  841.       lf.lfQuality        = DEFAULT_QUALITY;
  842.       lf.lfWeight         = FW_NORMAL;
  843.       lf.lfClipPrecision  = CLIP_LH_ANGLES | CLIP_STROKE_PRECIS;
  844.       lf.lfPitchAndFamily = FF_SWISS;
  845.       lf.lfEscapement     = 900;
  846.  
  847.       m_RowsTitleFont.CreateFontIndirect( &lf );
  848.    }
  849.    CATCH( CResourceException, e )
  850.    {
  851.       TRACE( "CLabeledGrid::m_SetColumnFontSize(), font creation failed\n" );
  852.       return;
  853.    }
  854.    END_CATCH
  855. }
  856.  
  857. void CLabeledGrid::PrepareForPainting( CDC&  device_context, 
  858.                                        DWORD row_font_size,
  859.                                        DWORD column_font_size,
  860.                                        DWORD rows_title_font_size,
  861.                                        DWORD columns_title_font_size )
  862. {
  863.    m_SetColumnFontSize( device_context, column_font_size );
  864.    m_SetRowFontSize( device_context, row_font_size );
  865.    m_SetColumnsTitleFontSize( device_context, columns_title_font_size );
  866.    m_SetRowsTitleFontSize( device_context, rows_title_font_size );
  867.  
  868.    CSize size;
  869.  
  870.    DWORD index         = 0;
  871.    DWORD biggest_value = 0;
  872.  
  873.    CFont *old_font = (CFont *) NULL;
  874.  
  875.    old_font = device_context.SelectObject( &m_RowsTitleFont );
  876.  
  877.    size = device_context.GetTextExtent( m_RowsTitle, m_RowsTitle.GetLength() );
  878.  
  879.    m_NumberOfLogicalUnitsInRowsTitle = size.cx;
  880.  
  881.    device_context.SelectObject( &m_ColumnsTitleFont );
  882.  
  883.    size = device_context.GetTextExtent( m_ColumnsTitle, m_ColumnsTitle.GetLength() );
  884.  
  885.    m_NumberOfLogicalUnitsInColumnsTitle = size.cx;
  886.  
  887.    device_context.SelectObject( &m_RowNamesFont );
  888.  
  889.    CString name;
  890.  
  891.    while( index < m_NumberOfRows )
  892.    {
  893.       GetRowName( index, name );
  894.  
  895.       if ( (DWORD) name.GetLength() > biggest_value )
  896.       {
  897.          size = device_context.GetTextExtent( name, name.GetLength() );
  898.       }
  899.  
  900.       index++;
  901.    }
  902.  
  903.    m_NumberOfLogicalUnitsInLongestRowName = size.cx;
  904.  
  905.    index         = 0;
  906.    biggest_value = 0;
  907.  
  908.    device_context.SelectObject( &m_ColumnNamesFont );
  909.  
  910.    while( index < m_NumberOfColumns )
  911.    {
  912.       GetColumnName( index, name );
  913.  
  914.       if ( (DWORD) name.GetLength() > biggest_value )
  915.       {
  916.          size = device_context.GetTextExtent( name, name.GetLength() );
  917.       }
  918.  
  919.       index++;
  920.    }
  921.  
  922.    m_NumberOfLogicalUnitsInLongestColumnName = size.cx;
  923.  
  924.    device_context.SelectObject( old_font );
  925. }
  926.  
  927. void CLabeledGrid::RemoveAll()
  928. {
  929.    CDrawingObjectGrid::RemoveAll();
  930.  
  931.    m_ColumnNames.RemoveAll();
  932.    m_RowNames.RemoveAll();
  933. }
  934.  
  935. void CLabeledGrid::Serialize( CArchive& archive )
  936. {
  937.    CDrawingObjectGrid::Serialize( archive );
  938.  
  939.    if ( archive.IsStoring() )
  940.    {
  941.       archive << m_ColumnsTitle;
  942.       archive << m_RowsTitle;
  943.    }
  944.    else
  945.    {
  946.       archive >> m_ColumnsTitle;
  947.       archive >> m_RowsTitle;
  948.    }
  949.  
  950.    m_ColumnNames.Serialize( archive );
  951.    m_RowNames.Serialize( archive );
  952. }
  953.  
  954. void CLabeledGrid::SetColumnName( DWORD column_number, LPCTSTR column_name )
  955. {
  956.    if ( column_number > m_NumberOfColumns )
  957.    {
  958.       return;
  959.    }
  960.  
  961.    if ( column_name == NULL )
  962.    {
  963.       m_ColumnNames.SetAt( column_number, "" );
  964.    }
  965.    else
  966.    {
  967.       m_ColumnNames.SetAt( column_number, column_name );
  968.    }
  969. }
  970.  
  971. void CLabeledGrid::SetColumnsTitle( LPCTSTR columns_title )
  972. {
  973.    if ( columns_title == NULL )
  974.    {
  975.       m_ColumnsTitle.Empty();
  976.    }
  977.    else
  978.    {
  979.       m_ColumnsTitle = columns_title;
  980.    }
  981. }
  982.  
  983. void CLabeledGrid::SetLabelOptions( DWORD options )
  984. {
  985.    m_LabelOptions = options;
  986. }
  987.  
  988. void CLabeledGrid::SetRowName( DWORD row_number, LPCTSTR row_name )
  989. {
  990.    if ( row_number > m_NumberOfRows )
  991.    {
  992.       return;
  993.    }
  994.  
  995.    if ( row_name == NULL )
  996.    {
  997.       m_RowNames.SetAt( row_number, "" );
  998.    }
  999.    else
  1000.    {
  1001.       m_RowNames.SetAt( row_number, row_name );
  1002.    }
  1003. }
  1004.  
  1005. void CLabeledGrid::SetRowsTitle( LPCTSTR rows_title )
  1006. {
  1007.    if ( rows_title == NULL )
  1008.    {
  1009.       m_RowsTitle.Empty();
  1010.    }
  1011.    else
  1012.    {
  1013.       m_RowsTitle = rows_title;
  1014.    }
  1015. }
  1016.  
  1017. void CLabeledGrid::SetSize( DWORD number_of_rows, DWORD number_of_columns )
  1018. {
  1019.    CDrawingObjectGrid::SetSize( number_of_rows, number_of_columns );
  1020.  
  1021.    m_ColumnNames.SetSize( m_NumberOfColumns );
  1022.    m_RowNames.SetSize( m_NumberOfRows );
  1023. }
  1024.